###########################################
#REPLICATION FILE FOR VOX POPULI, VOX DEI?
#by ADAM RAMEY
#SECTION 5 (incl. FIGURES 3 & 7)
###########################################



library(foreign)
library(Zelig)
library(ZeligChoice)
library(MASS)
library(MCMCpack)
library(reshape)
library(basicspace)

##########################################
#NOTE: This file assumes you have already
#run the estimation for 2010 and 2012
#and that you attached the estimated pos-
#itions to the dataframes being loaded.
#In the replication file, I have included
#the estimates in the 2010 and 2012 data
#frames.
##########################################


##Load in the 2012 and 2010 data separately...

##be sure to change the line below so as to indicate where the data and scripts are
setwd("~/Downloads/Replication Materials/")
load("CCES 2010.Rda")
cces2010 <- cces

load("CCES 2012.Rda")
cces2012 <- cces

##################################
#COMPARING 2010-2012: Linear Map
#
#Assume that dem & rep parties are
#the same
#
#Map 2012 D,R to 2010 D,R and re-
#scale all accordingly
##################################

##set up 2010 scale 

##define the actors based on which CCES question
self <- cces2010$CC334A
obama <- cces2010$CC334C
democrats <- cces2010$CC334D
republicans <- cces2010$CC334E

##put them in a matrix
placement_matrix_2010 <- cbind(self, obama, democrats, republicans)

##recode missing values to 999
for (j in 1:ncol(placement_matrix_2010)){
  placement_matrix_2010[,j] <- as.numeric(placement_matrix_2010[,j])
  placement_matrix_2010[,j] <- replace(placement_matrix_2010[,j], placement_matrix_2010[,j]==8 | is.na(placement_matrix_2010[,j]), 999)
}

##set up 2012 scale in the same way

self <- cces2012$CC334A
obama <- cces2012$CC334C
democrats <- cces2012$CC334E
republicans <- cces2012$CC334F

placement_matrix_2012 <- cbind(self, obama, democrats, republicans)

for (j in 1:ncol(placement_matrix_2012)){
  placement_matrix_2012[,j] <- as.numeric(placement_matrix_2012[,j])
  placement_matrix_2012[,j] <- replace(placement_matrix_2012[,j], placement_matrix_2012[,j]==8 | is.na(placement_matrix_2012[,j]), 999)
}

##now, run A-M separately by year

am2010 <- aldmck(placement_matrix_2010, polarity=2, respondent=1, missing=999, verbose=TRUE)
am2012 <- aldmck(placement_matrix_2012, polarity=2, respondent=1, missing=999, verbose=TRUE)

#save the overall dem and rep pos BY YEAR

dem2010 <- am2010$stim[2]
rep2010 <- am2010$stim[3]

dem2012 <- am2012$stim[2]
rep2012 <- am2012$stim[3]

##Now, define the linear mapping function

linmap <- function(x,D0,R0,D1,R1){
              slope <- (R1-D1)/(R0-D0)
              rescaling <- slope*x - slope*D1 + D0
              return(rescaling)
}

##Map the 2010 data to the 2012 space

respondent_ideal_point_2010 <- linmap(cces2010$respondent_ideal_point, 
                                      D0=dem2010, R0=rep2010, 
                                      D1=dem2012, R1=rep2012)

house_scale_2010 <- linmap(cces2010$house_scale, 
                                      D0=dem2010, R0=rep2010, 
                                      D1=dem2012, R1=rep2012)

gov_scale_2010 <- linmap(cces2010$gov, 
                                      D0=dem2010, R0=rep2010, 
                                      D1=dem2012, R1=rep2012)

sen1_2010 <- linmap(cces2010$sen1, 
                                      D0=dem2010, R0=rep2010, 
                                      D1=dem2012, R1=rep2012)

sen2_2010 <- linmap(cces2010$sen2, 
                                      D0=dem2010, R0=rep2010, 
                                      D1=dem2012, R1=rep2012)


##We must now group the results by state and/or district

#house scores 2010 and then 2012
h2010 <- tapply(house_scale_2010, cces2010$cong, mean, na.rm=TRUE)
h2012 <- tapply(cces2012$house_scale, cces2012$cong, mean, na.rm=TRUE)

#senate scores 2010 and then 2012
s2010 <- c(tapply(sen1_2010, cces2010$V206, mean, na.rm=TRUE), tapply(sen2_2010, cces2010$V206, mean, na.rm=TRUE))
s2012 <- c(tapply(cces2012$sen1, cces2012$inputstate, mean, na.rm=TRUE), tapply(cces2012$sen2, cces2012$inputstate, mean, na.rm=TRUE))

#mean district voter 2010 and then 2012
cong_voter2010 <- tapply(respondent_ideal_point_2010, cces2010$cong, mean, na.rm=TRUE)
cong_voter2012 <- tapply(cces2012$respondent_ideal_point, cces2012$cong, mean, na.rm=TRUE)

#mean state voter 2010 and then 2012
state_voter2010 <- tapply(respondent_ideal_point_2010, cces2010$V206, mean, na.rm=TRUE)
state_voter2012 <- tapply(cces2012$respondent_ideal_point, cces2012$inputstate, mean, na.rm=TRUE)

##We can now replicated Figures 3 and 7
#first, figure 3
dist_level <- data.frame(cong_voter2010,cong_voter2012)
state_level <- data.frame(state_voter2010,state_voter2012)

g1 <- ggplot(dist_level, aes(cong_voter2010,cong_voter2012)) +
  geom_point() + 
  theme_bw() + 
  xlab("District Mean Voter (2010)") +
  ylab("District Mean Voter (2012)")
  
g2 <- ggplot(state_level[c(1:8,10:51),], aes(state_voter2010,state_voter2012)) +
  geom_point() + 
  theme_bw() + 
  xlab("State Mean Voter (2010)") +
  ylab("State Mean Voter (2012)")

ggsave(g1, file="distplot.png",
       height=6, width=8)

ggsave(g2, file="stateplot.png",
       height=6, width=8)
  
#second, figure 7
r1 <- data.frame(voter=c(cong_voter2010,cong_voter2012), pol=c(h2010,h2012),
                 year=rep(c(2010,2012), each=length(h2010)))

g3 <- ggplot(subset(r1,year==2010), aes(voter,pol)) +
  geom_point() +
  stat_smooth() +
  theme_bw() +
  xlab("District Mean Voter (2010)") +
  ylab("Legislator Ideal Point (2012)")

g4 <- ggplot(subset(r1,year==2012), aes(voter,pol)) +
  geom_point() +
  stat_smooth() +
  theme_bw() +
  xlab("District Mean Voter (2010)") +
  ylab("Legislator Ideal Point (2012)")

ggsave(g3, file="rep2010.png",
       height=6, width=8)

ggsave(g4, file="rep2012.png",
       height=6, width=8)


